package jp.sourceforge.akjrcp.util;

import org.eclipse.swt.graphics.RGB;

public class ColorUtils {

	public static RGB hexToRGB(String hex){
		if(hex==null || hex.length()==0){
			return null;
		}
		int value=hexToInt(hex);
		int r=(0xff0000&value)>>16;
		int g=(0x00ff00&value)>>8;
		int b=(0xff&value);
		return new RGB(r,g,b);
	}
	
	public static String rgbToHex(RGB rgb){
		if(rgb==null){
			return null;
		}
		String r=Integer.toHexString(rgb.red);
		if(r.length()<2){
			r="0"+r;
		}
		String g=Integer.toHexString(rgb.green);
		if(g.length()<2){
			g="0"+g;
		}
		String b=Integer.toHexString(rgb.blue);
		if(b.length()<2){
			b="0"+b;
		}
		return "#"+r+g+b;
	}
	
	public static int hexToInt(String hex){
			String h=hex;
			if(h.startsWith("#")){
				h=hex.substring(1);
			}
		return Integer.parseInt(h, 16);
	}
}
